using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Vision.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace SilverSea.Video.Tools
{
	public class Camera
	{
		public IVideoSourcePlayer videoSourcePlayer;
		private NewFrameEventHandler newFrameEventHandler_0;
		private EventHandler eventHandler_0;
		private MotionAnalyzingEventHandler motionAnalyzingEventHandler_0;
		private VideoWriterFinishEventHandler videoWriterFinishEventHandler_0;
		private CameraErrorEventHandler cameraErrorEventHandler_0;
		private PTZSource ptzsource_0;
		//private int int_0;
		public MotionDetector detector;
		private System.Windows.Forms.Timer timer_0 = new System.Windows.Forms.Timer();
		private int int_1;
		private System.Windows.Forms.Timer timer_1 = new System.Windows.Forms.Timer();
		private BackgroundWorker backgroundWorker_0;
		private System.Windows.Forms.Timer timer_2 = new System.Windows.Forms.Timer();
		private string string_0 = string.Empty;
		private TextOverlayLocation textOverlayLocation_0 = TextOverlayLocation.BottomLeft;
		private System.Drawing.Color color_0 = System.Drawing.Color.Blue;
		private object object_0 = new object();
		private int int_2;
		private string string_1 = "Camera";
		private string string_2 = "video source";
		private int int_3;
		private string string_3 = string.Empty;
		private string string_4 = string.Empty;
		private CameraType cameraType_0;
		private StreamType streamType_0 = StreamType.mjpeg;
		private ProtocolType protocolType_0;
		private string string_5 = "640x480";
		private string string_6 = "Standard";
		private int int_4 = 30;
		private string string_7 = string.Empty;
		private bool bool_0;
		private bool bool_1;
		private int int_5 = 15;
		private string string_8 = string.Empty;
		private bool bool_2;
		private bool bool_3 = true;
		private int int_6 = 40;
		private System.Drawing.Rectangle[] rectangle_0;
		private string string_9 = "Deinterlace Filter";
		private bool bool_4;
		private int int_7;
		private bool bool_5;
		private string string_10 = "wmv3";
		private int int_8 = 5;
		private string string_11 = string.Empty;
		private int int_9 = 30;
		private string string_12 = string.Empty;
		public event NewFrameEventHandler NewFrame
		{
			add
			{
				NewFrameEventHandler newFrameEventHandler = this.newFrameEventHandler_0;
				NewFrameEventHandler newFrameEventHandler2;
				do
				{
					newFrameEventHandler2 = newFrameEventHandler;
					NewFrameEventHandler value2 = (NewFrameEventHandler)Delegate.Combine(newFrameEventHandler2, value);
					newFrameEventHandler = Interlocked.CompareExchange<NewFrameEventHandler>(ref this.newFrameEventHandler_0, value2, newFrameEventHandler2);
				}
				while (newFrameEventHandler != newFrameEventHandler2);
			}
			remove
			{
				NewFrameEventHandler newFrameEventHandler = this.newFrameEventHandler_0;
				NewFrameEventHandler newFrameEventHandler2;
				do
				{
					newFrameEventHandler2 = newFrameEventHandler;
					NewFrameEventHandler value2 = (NewFrameEventHandler)Delegate.Remove(newFrameEventHandler2, value);
					newFrameEventHandler = Interlocked.CompareExchange<NewFrameEventHandler>(ref this.newFrameEventHandler_0, value2, newFrameEventHandler2);
				}
				while (newFrameEventHandler != newFrameEventHandler2);
			}
		}
		public event EventHandler Alarm
		{
			add
			{
				EventHandler eventHandler = this.eventHandler_0;
				EventHandler eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler value2 = (EventHandler)Delegate.Combine(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_0, value2, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
			remove
			{
				EventHandler eventHandler = this.eventHandler_0;
				EventHandler eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler value2 = (EventHandler)Delegate.Remove(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_0, value2, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
		}
		public event MotionAnalyzingEventHandler MotionAnalyzing
		{
			add
			{
				MotionAnalyzingEventHandler motionAnalyzingEventHandler = this.motionAnalyzingEventHandler_0;
				MotionAnalyzingEventHandler motionAnalyzingEventHandler2;
				do
				{
					motionAnalyzingEventHandler2 = motionAnalyzingEventHandler;
					MotionAnalyzingEventHandler value2 = (MotionAnalyzingEventHandler)Delegate.Combine(motionAnalyzingEventHandler2, value);
					motionAnalyzingEventHandler = Interlocked.CompareExchange<MotionAnalyzingEventHandler>(ref this.motionAnalyzingEventHandler_0, value2, motionAnalyzingEventHandler2);
				}
				while (motionAnalyzingEventHandler != motionAnalyzingEventHandler2);
			}
			remove
			{
				MotionAnalyzingEventHandler motionAnalyzingEventHandler = this.motionAnalyzingEventHandler_0;
				MotionAnalyzingEventHandler motionAnalyzingEventHandler2;
				do
				{
					motionAnalyzingEventHandler2 = motionAnalyzingEventHandler;
					MotionAnalyzingEventHandler value2 = (MotionAnalyzingEventHandler)Delegate.Remove(motionAnalyzingEventHandler2, value);
					motionAnalyzingEventHandler = Interlocked.CompareExchange<MotionAnalyzingEventHandler>(ref this.motionAnalyzingEventHandler_0, value2, motionAnalyzingEventHandler2);
				}
				while (motionAnalyzingEventHandler != motionAnalyzingEventHandler2);
			}
		}
		public event VideoWriterFinishEventHandler VideoWriterFinish
		{
			add
			{
				VideoWriterFinishEventHandler videoWriterFinishEventHandler = this.videoWriterFinishEventHandler_0;
				VideoWriterFinishEventHandler videoWriterFinishEventHandler2;
				do
				{
					videoWriterFinishEventHandler2 = videoWriterFinishEventHandler;
					VideoWriterFinishEventHandler value2 = (VideoWriterFinishEventHandler)Delegate.Combine(videoWriterFinishEventHandler2, value);
					videoWriterFinishEventHandler = Interlocked.CompareExchange<VideoWriterFinishEventHandler>(ref this.videoWriterFinishEventHandler_0, value2, videoWriterFinishEventHandler2);
				}
				while (videoWriterFinishEventHandler != videoWriterFinishEventHandler2);
			}
			remove
			{
				VideoWriterFinishEventHandler videoWriterFinishEventHandler = this.videoWriterFinishEventHandler_0;
				VideoWriterFinishEventHandler videoWriterFinishEventHandler2;
				do
				{
					videoWriterFinishEventHandler2 = videoWriterFinishEventHandler;
					VideoWriterFinishEventHandler value2 = (VideoWriterFinishEventHandler)Delegate.Remove(videoWriterFinishEventHandler2, value);
					videoWriterFinishEventHandler = Interlocked.CompareExchange<VideoWriterFinishEventHandler>(ref this.videoWriterFinishEventHandler_0, value2, videoWriterFinishEventHandler2);
				}
				while (videoWriterFinishEventHandler != videoWriterFinishEventHandler2);
			}
		}
		public event CameraErrorEventHandler CameraError
		{
			add
			{
				CameraErrorEventHandler cameraErrorEventHandler = this.cameraErrorEventHandler_0;
				CameraErrorEventHandler cameraErrorEventHandler2;
				do
				{
					cameraErrorEventHandler2 = cameraErrorEventHandler;
					CameraErrorEventHandler value2 = (CameraErrorEventHandler)Delegate.Combine(cameraErrorEventHandler2, value);
					cameraErrorEventHandler = Interlocked.CompareExchange<CameraErrorEventHandler>(ref this.cameraErrorEventHandler_0, value2, cameraErrorEventHandler2);
				}
				while (cameraErrorEventHandler != cameraErrorEventHandler2);
			}
			remove
			{
				CameraErrorEventHandler cameraErrorEventHandler = this.cameraErrorEventHandler_0;
				CameraErrorEventHandler cameraErrorEventHandler2;
				do
				{
					cameraErrorEventHandler2 = cameraErrorEventHandler;
					CameraErrorEventHandler value2 = (CameraErrorEventHandler)Delegate.Remove(cameraErrorEventHandler2, value);
					cameraErrorEventHandler = Interlocked.CompareExchange<CameraErrorEventHandler>(ref this.cameraErrorEventHandler_0, value2, cameraErrorEventHandler2);
				}
				while (cameraErrorEventHandler != cameraErrorEventHandler2);
			}
		}
		public int ID
		{
			get
			{
				return this.int_2;
			}
			set
			{
				this.int_2 = value;
			}
		}
		public string Name
		{
			get
			{
				return this.string_1;
			}
			set
			{
				this.string_1 = value;
			}
		}
		public string VideoSource
		{
			get
			{
				return this.string_2;
			}
			set
			{
				this.string_2 = value;
			}
		}
		public int Chanel
		{
			get
			{
				return this.int_3;
			}
			set
			{
				this.int_3 = value;
			}
		}
		public string Login
		{
			get
			{
				return this.string_3;
			}
			set
			{
				this.string_3 = value;
			}
		}
		public string Password
		{
			get
			{
				return this.string_4;
			}
			set
			{
				this.string_4 = value;
			}
		}
		public CameraType CameraType
		{
			get
			{
				return this.cameraType_0;
			}
			set
			{
				this.cameraType_0 = value;
			}
		}
		public StreamType StreamType
		{
			get
			{
				return this.streamType_0;
			}
			set
			{
				this.streamType_0 = value;
			}
		}
		public ProtocolType ProtocolType
		{
			get
			{
				return this.protocolType_0;
			}
			set
			{
				this.protocolType_0 = value;
			}
		}
		public string Resolution
		{
			get
			{
				return this.string_5;
			}
			set
			{
				this.string_5 = value;
			}
		}
		public string Quality
		{
			get
			{
				return this.string_6;
			}
			set
			{
				this.string_6 = value;
			}
		}
		public int FrameRate
		{
			get
			{
				return this.int_4;
			}
			set
			{
				this.int_4 = value;
			}
		}
		public string Description
		{
			get
			{
				return this.string_7;
			}
			set
			{
				this.string_7 = value;
			}
		}
		public float FPS
		{
			get
			{
				if (this.videoSourcePlayer != null)
				{
					return this.videoSourcePlayer.FPS;
				}
				return 0f;
			}
		}
		public float BPS
		{
			get
			{
				if (this.videoSourcePlayer != null)
				{
					return this.videoSourcePlayer.BPS;
				}
				return 0f;
			}
		}
		public bool EnableAUXControl
		{
			get
			{
				return this.bool_0;
			}
			set
			{
				this.bool_0 = value;
			}
		}
		public bool EnableMotionDetection
		{
			get
			{
				return this.bool_1;
			}
			set
			{
				this.bool_1 = value;
				this.timer_0.Tick -= new EventHandler(this.timer_0_Tick);
				this.timer_0.Stop();
				this.timer_1.Tick -= new EventHandler(this.timer_1_Tick);
				this.timer_1.Stop();
				if (this.detector != null)
				{
					this.detector.Reset();
					this.detector = null;
				}
				if (this.bool_1)
				{
					this.detector = new MotionDetector(new TwoFramesDifferenceDetector(this.bool_3), null);
					this.detector.MotionZones = this.rectangle_0;
					this.timer_0.Tick += new EventHandler(this.timer_0_Tick);
					this.timer_0.Start();
					if (this.int_6 > 0)
					{
						this.timer_1.Interval = this.int_6;
						this.timer_1.Tick += new EventHandler(this.timer_1_Tick);
						this.timer_1.Start();
					}
				}
			}
		}
		public int MotionAlarmLevel
		{
			get
			{
				return this.int_5;
			}
			set
			{
				this.int_5 = value;
			}
		}
		public string MotionRegions
		{
			get
			{
				return this.string_8;
			}
			set
			{
				this.string_8 = value;
				this.rectangle_0 = this.GetMotionRectangles();
				if (this.detector != null)
				{
					this.detector.MotionZones = this.rectangle_0;
				}
				if (this.videoSourcePlayer != null)
				{
					if (this.bool_2)
					{
						this.videoSourcePlayer.MotionRectangles = this.rectangle_0;
						return;
					}
					this.videoSourcePlayer.MotionRectangles = null;
				}
			}
		}
		public bool DisplayMotionRegion
		{
			get
			{
				return this.bool_2;
			}
			set
			{
				this.bool_2 = value;
				if (this.videoSourcePlayer != null)
				{
					if (this.bool_2)
					{
						this.videoSourcePlayer.MotionRectangles = this.rectangle_0;
						return;
					}
					this.videoSourcePlayer.MotionRectangles = null;
				}
			}
		}
		public bool SuppressNoise
		{
			get
			{
				return this.bool_3;
			}
			set
			{
				this.bool_3 = value;
			}
		}
		public int MotionDetectionInterval
		{
			get
			{
				return this.int_6;
			}
			set
			{
				this.int_6 = value;
			}
		}
		public System.Drawing.Rectangle[] MotionRectangles
		{
			get
			{
				return this.rectangle_0;
			}
		}
		public string DeinterlaceFilterName
		{
			get
			{
				return this.string_9;
			}
			set
			{
				this.string_9 = value;
			}
		}
		public bool UsingDeinterlaceFilter
		{
			get
			{
				return this.bool_4;
			}
			set
			{
				this.bool_4 = value;
			}
		}
		public int UsingPlugins
		{
			get
			{
				return this.int_7;
			}
			set
			{
				this.int_7 = value;
			}
		}
		public bool IsRunning
		{
			get
			{
				return this.videoSourcePlayer != null && this.videoSourcePlayer.IsRunning;
			}
		}
		public string TextOverlay
		{
			get
			{
				return this.string_0;
			}
			set
			{
				this.string_0 = value;
			}
		}
		public TextOverlayLocation TextOverlayLocation
		{
			get
			{
				return this.textOverlayLocation_0;
			}
			set
			{
				this.textOverlayLocation_0 = value;
			}
		}
		public System.Drawing.Color TextOverlayColor
		{
			get
			{
				return this.color_0;
			}
			set
			{
				this.color_0 = value;
			}
		}
		public DateTime LastTimeReceivedFrame
		{
			get
			{
				if (this.videoSourcePlayer != null)
				{
					return this.videoSourcePlayer.LastTimeReceivedFrame;
				}
				return DateTime.Now;
			}
			set
			{
				if (this.videoSourcePlayer != null)
				{
					this.videoSourcePlayer.LastTimeReceivedFrame = value;
				}
			}
		}
		public bool EnableRecording
		{
			get
			{
				return this.bool_5;
			}
			set
			{
				this.bool_5 = value;
			}
		}
		public string RecordingCodec
		{
			get
			{
				return this.string_10;
			}
			set
			{
				this.string_10 = value;
			}
		}
		public int RecordingRate
		{
			get
			{
				return this.int_8;
			}
			set
			{
				this.int_8 = value;
			}
		}
		public string RecordingSize
		{
			get
			{
				return this.string_11;
			}
			set
			{
				this.string_11 = value;
			}
		}
		public int VideoLengthLimit
		{
			get
			{
				return this.int_9;
			}
			set
			{
				this.int_9 = value;
			}
		}
		public string VideoFolder
		{
			get
			{
				return this.string_12;
			}
			set
			{
				this.string_12 = value;
			}
		}
		public System.Drawing.Rectangle[] GetMotionRectangles()
		{
			System.Drawing.Rectangle[] array = null;
			if (this.string_8 != string.Empty)
			{
				List<System.Drawing.Rectangle> list = new List<System.Drawing.Rectangle>();
				System.Drawing.Rectangle item = default(System.Drawing.Rectangle);
				string[] array2 = this.string_8.Split(new char[]
				{
					';'
				});
				string[] array3 = array2;
				for (int i = 0; i < array3.Length; i++)
				{
					string text = array3[i];
					string[] array4 = text.Split(new char[]
					{
						','
					});
					if (array4.Length == 4)
					{
						try
						{
							item = new System.Drawing.Rectangle(int.Parse(array4[0]), int.Parse(array4[1]), int.Parse(array4[2]), int.Parse(array4[3]));
							list.Add(item);
						}
						catch (Exception ex)
						{
							if (this.cameraErrorEventHandler_0 != null)
							{
								this.cameraErrorEventHandler_0(this, "GetMotionRectangle: " + ex.Message);
							}
						}
					}
				}
				array = new System.Drawing.Rectangle[list.Count];
				list.CopyTo(array);
			}
			return array;
		}
		public string GetMotionRegions(System.Drawing.Rectangle[] rectangles)
		{
			string text = string.Empty;
			if (rectangles != null && rectangles.Length > 0)
			{
				int num = 0;
				for (int i = 0; i < rectangles.Length; i++)
				{
					System.Drawing.Rectangle rectangle = rectangles[i];
					object obj = text;
					text = string.Concat(new object[]
					{
						obj,
						rectangle.X,
						",",
						rectangle.Y,
						",",
						rectangle.Width,
						",",
						rectangle.Height
					});
					num++;
					if (num < rectangles.Length)
					{
						text += ";";
					}
				}
			}
			return text;
		}
		public void SaveCurrentVideoFrame(string path)
		{
			if (this.videoSourcePlayer != null)
			{
				this.videoSourcePlayer.SaveCurrentVideoFrame(path);
			}
		}
		public System.Drawing.Bitmap GetCurrentVideoFrame()
		{
			if (this.videoSourcePlayer != null)
			{
				return this.videoSourcePlayer.GetCurrentVideoFrame();
			}
			return null;
		}
		public UnmanagedImage GetCurrentUnmanagedVideoFrame()
		{
			if (this.videoSourcePlayer != null)
			{
				return this.videoSourcePlayer.GetCurrentUnmanagedVideoFrame();
			}
			return null;
		}
		public void Start()
		{
			switch (this.cameraType_0)
			{
			case CameraType.Panasonic:
			{
				IVideoSource videoSource = null;
				if (this.streamType_0 == StreamType.jpeg)
				{
					videoSource = new JPEGStream();
					videoSource.Source = string.Concat(new string[]
					{
						"http://",
						this.string_2,
						"/SnapshotJPEG?Resolution=",
						this.string_5,
						"&Quality=",
						this.string_6
					});
					int[] array = new int[]
					{
						0,
						1000,
						500,
						333,
						250,
						200,
						167,
						142,
						125,
						111,
						100,
						90,
						83,
						76,
						71,
						67,
						63,
						58,
						55,
						52,
						50,
						47,
						45,
						43,
						41,
						40,
						38,
						37,
						35,
						34,
						33
					};
					((JPEGStream)videoSource).FrameInterval = array[this.int_4];
				}
				else
				{
					if (this.streamType_0 == StreamType.mjpeg)
					{
						if (this.int_7 == 0)
						{
							videoSource = new MJPEGStream();
						}
						else
						{
							videoSource = new VLCDecoderSource();
							int width = 640;
							int height = 480;
							string[] array2 = this.string_5.Split(new char[]
							{
								'x'
							});
							if (array2.Length == 2 && (!int.TryParse(array2[0], out width) || !int.TryParse(array2[1], out height)))
							{
								width = 640;
								height = 480;
							}
							videoSource.DesiredFrameSize = new System.Drawing.Size(width, height);
						}
						videoSource.Source = string.Concat(new string[]
						{
							"http://",
							this.string_2,
							"/nphMotionJpeg?Resolution=",
							this.string_5,
							"&Quality=",
							this.string_6
						});
						if (this.int_4 > 0)
						{
							videoSource.Source = string.Concat(new object[]
							{
								"http://",
								this.string_2,
								"/nphMotionJpeg?Resolution=",
								this.string_5,
								"&Quality=",
								this.string_6,
								"&Framerate=",
								this.int_4
							});
						}
					}
					else
					{
						if (this.streamType_0 == StreamType.mpeg4)
						{
							videoSource = new VLCDecoderSource();
							int width2 = 640;
							int height2 = 480;
							string[] array3 = this.string_5.Split(new char[]
							{
								'x'
							});
							if (array3.Length == 2 && (!int.TryParse(array3[0], out width2) || !int.TryParse(array3[1], out height2)))
							{
								width2 = 640;
								height2 = 480;
							}
							videoSource.DesiredFrameSize = new System.Drawing.Size(width2, height2);
							if (this.protocolType_0 == ProtocolType.http)
							{
								videoSource.Source = "http://" + this.string_2 + "/rtpOverHttp?Url=nphMpeg4/nil-" + this.string_5;
							}
							else
							{
								videoSource.Source = "rtsp://" + this.string_2 + "/nphMpeg4/nil-" + this.string_5;
							}
						}
						else
						{
							if (this.streamType_0 == StreamType.h264)
							{
								videoSource = new VLCDecoderSource();
								int width3 = 640;
								int height3 = 480;
								string[] array4 = this.string_5.Split(new char[]
								{
									'x'
								});
								if (array4.Length == 2 && (!int.TryParse(array4[0], out width3) || !int.TryParse(array4[1], out height3)))
								{
									width3 = 640;
									height3 = 480;
								}
								videoSource.DesiredFrameSize = new System.Drawing.Size(width3, height3);
								if (this.protocolType_0 == ProtocolType.http)
								{
									videoSource.Source = "http://" + this.string_2 + "/nphH264AACauth?Resolution=" + this.string_5;
								}
								else
								{
									videoSource.Source = "rtsp://" + this.string_2 + "/nphH264/nil-" + this.string_5;
								}
							}
						}
					}
				}
				if (videoSource != null)
				{
					videoSource.Login = this.string_3;
					videoSource.Password = this.string_4;
					this.method_0(videoSource);
					return;
				}
				break;
			}
			case CameraType.PanasonicPro:
			{
				IVideoSource videoSource2 = null;
				string text = this.string_5;
				if (!(this.string_5 == "320x240") && !(this.string_5 == "320x180"))
				{
					if (!(this.string_5 == "640x480") && !(this.string_5 == "640x360"))
					{
						if (this.string_5 == "800x600")
						{
							text = "800";
						}
						else
						{
							if (this.string_5 == "960x720")
							{
								text = "960";
							}
							else
							{
								if (!(this.string_5 == "1280x960") && !(this.string_5 == "1280x720"))
								{
									if (this.string_5 == "1920x1080")
									{
										text = "1920";
									}
									else
									{
										if (this.string_5 == "2048x1536")
										{
											text = "2048";
										}
									}
								}
								else
								{
									text = "1280";
								}
							}
						}
					}
					else
					{
						text = "640";
					}
				}
				else
				{
					text = "320";
				}
				if (this.streamType_0 == StreamType.jpeg)
				{
					videoSource2 = new JPEGStream();
					videoSource2.Source = string.Concat(new string[]
					{
						"http://",
						this.string_2,
						"/cgi-bin/camera?resolution=",
						text,
						"&page=20040830203157"
					});
					int[] array5 = new int[]
					{
						0,
						1000,
						500,
						333,
						250,
						200,
						167,
						142,
						125,
						111,
						100,
						90,
						83,
						76,
						71,
						67,
						63,
						58,
						55,
						52,
						50,
						47,
						45,
						43,
						41,
						40,
						38,
						37,
						35,
						34,
						33
					};
					((JPEGStream)videoSource2).FrameInterval= array5[this.int_4];
				}
				else
				{
					if (this.streamType_0 == StreamType.mjpeg)
					{
						if (this.int_7 == 0)
						{
							videoSource2 = new MJPEGStream();
						}
						else
						{
							videoSource2 = new VLCDecoderSource();
							int width4 = 640;
							int height4 = 480;
							string[] array6 = this.string_5.Split(new char[]
							{
								'x'
							});
							if (array6.Length == 2 && (!int.TryParse(array6[0], out width4) || !int.TryParse(array6[1], out height4)))
							{
								width4 = 640;
								height4 = 480;
							}
							videoSource2.DesiredFrameSize = new System.Drawing.Size(width4, height4);
						}
						string text2 = "http://" + this.string_2 + "/cgi-bin/mjpeg?resolution=" + this.string_5;
						if (this.int_4 > 0)
						{
							text2 = text2 + "&framerate=" + this.int_4;
						}
						if (this.int_3 > 0)
						{
							text2 = text2 + "&ch=" + this.int_3;
						}
						videoSource2.Source = text2;
					}
					else
					{
						if (this.streamType_0 == StreamType.mpeg4)
						{
							videoSource2 = new VLCDecoderSource();
							int width5 = 640;
							int height5 = 480;
							string[] array7 = this.string_5.Split(new char[]
							{
								'x'
							});
							if (array7.Length == 2 && (!int.TryParse(array7[0], out width5) || !int.TryParse(array7[1], out height5)))
							{
								width5 = 640;
								height5 = 480;
							}
							videoSource2.DesiredFrameSize = new System.Drawing.Size(width5, height5);
							videoSource2.Source = "rtsp://" + this.string_2 + "/MediaInput/mpeg4";
						}
						else
						{
							if (this.streamType_0 == StreamType.h264)
							{
								videoSource2 = new VLCDecoderSource();
								int width6 = 640;
								int height6 = 480;
								string[] array8 = this.string_5.Split(new char[]
								{
									'x'
								});
								if (array8.Length == 2 && (!int.TryParse(array8[0], out width6) || !int.TryParse(array8[1], out height6)))
								{
									width6 = 640;
									height6 = 480;
								}
								videoSource2.DesiredFrameSize = new System.Drawing.Size(width6, height6);
								videoSource2.Source = "rtsp://" + this.string_2 + "/MediaInput/h264";
							}
						}
					}
				}
				if (videoSource2 != null)
				{
					videoSource2.Login = this.string_3;
					videoSource2.Password = this.string_4;
					this.method_0(videoSource2);
					return;
				}
				break;
			}
			case CameraType.VideoCaptureDevice:
			{
				IVideoSource videoSource3;
				if (this.int_7 == 0)
				{
					videoSource3 = new VideoCaptureDevice();
				}
				else
				{
					videoSource3 = new VLC_VideoCaptureDevice();
				}
				if (videoSource3 != null)
				{
					videoSource3.Source = this.string_2;
					if (this.int_4 > 0)
					{
						videoSource3.DesiredFrameRate = this.int_4;
					}
					int width7 = 0;
					int height7 = 0;
					string[] array9 = this.string_5.Split(new char[]
					{
						'x'
					});
					if (array9.Length == 2 && (!int.TryParse(array9[0], out width7) || !int.TryParse(array9[1], out height7)))
					{
						width7 = 0;
						height7 = 0;
					}
					System.Drawing.Size desiredFrameSize = new System.Drawing.Size(width7, height7);
					if (desiredFrameSize.Width > 0 && desiredFrameSize.Height > 0)
					{
						videoSource3.DesiredFrameSize = desiredFrameSize;
					}
					if (videoSource3 is VideoCaptureDevice)
					{
						((VideoCaptureDevice)videoSource3).DeinterlaceFilterName = this.string_9;
                        ((VideoCaptureDevice)videoSource3).UsingDeInterlaceFilter = this.bool_4;
					}
					else
					{
						if (videoSource3 is VLC_VideoCaptureDevice)
						{
							((VLC_VideoCaptureDevice)videoSource3).DeinterlaceFilter = this.string_9;
							((VLC_VideoCaptureDevice)videoSource3).UsingDeInterlace = this.bool_4;
							((VLC_VideoCaptureDevice)videoSource3).Chanel = this.int_3;
						}
					}
					this.method_0(videoSource3);
					return;
				}
				break;
			}
			case CameraType.VideoFile:
				if (File.Exists(this.string_2))
				{
					IVideoSource ivideoSource_;
					if (this.int_7 == 0)
					{
						ivideoSource_ = new FileVideoSource(this.string_2);
					}
					else
					{
						ivideoSource_ = new VLC_FileVideoSource(this.string_2);
					}
					this.method_0(ivideoSource_);
				}
				break;
			default:
				return;
			}
		}
		private void method_0(IVideoSource ivideoSource_0)
		{
			this.method_1();
			if (this.videoSourcePlayer == null)
			{
				if (this.cameraType_0 != CameraType.VideoCaptureDevice && this.cameraType_0 != CameraType.VideoFile && this.streamType_0 == StreamType.mjpeg && this.int_7 == 2)
				{
					this.videoSourcePlayer = new AMCPlayer();
				}
				else
				{
					if (this.cameraType_0 == CameraType.VideoCaptureDevice && this.int_7 == 2)
					{
						this.videoSourcePlayer = new VLCPlayer();
					}
					else
					{
						this.videoSourcePlayer = new VideoSourcePlayer();
					}
				}
			}
			this.videoSourcePlayer.VideoSource = ivideoSource_0;
			this.videoSourcePlayer.MotionRectangleLineWidth = 2;
			if (this.bool_2)
			{
				this.videoSourcePlayer.MotionRectangles = this.rectangle_0;
			}
			else
			{
				this.videoSourcePlayer.MotionRectangles = null;
			}
			this.videoSourcePlayer.TextOverlay = this.string_0;
			this.videoSourcePlayer.TextOverlayLocation = this.textOverlayLocation_0;
			this.videoSourcePlayer.TextOverlayColor = this.color_0;
			this.videoSourcePlayer.NewFrame += new NewFrameHandler(this.method_2);
			this.videoSourcePlayer.VideoSourceError += new VideoSourceErrorEventHandler(this.videoSourcePlayer_VideoSourceError);
			this.videoSourcePlayer.Start();
			this.timer_2.Interval = 1000;
			this.timer_2.Tick += new EventHandler(this.timer_2_Tick);
			this.timer_2.Start();
			this.method_3();
			if (this.bool_1)
			{
				this.detector = new MotionDetector(new TwoFramesDifferenceDetector(this.bool_3), null);
                this.detector.MotionZones = this.rectangle_0;
				this.timer_0.Tick += new EventHandler(this.timer_0_Tick);
				this.timer_0.Start();
				if (this.int_6 > 0)
				{
					this.timer_1.Interval = this.int_6;
					this.timer_1.Tick += new EventHandler(this.timer_1_Tick);
					this.timer_1.Start();
				}
			}
		}
		public void SignalToStop()
		{
			if (this.videoSourcePlayer != null)
			{
				this.videoSourcePlayer.SignalToStop();
			}
		}
		public void Stop()
		{
			this.method_1();
		}
		private void method_1()
		{
			this.timer_2.Tick -= new EventHandler(this.timer_2_Tick);
			this.timer_2.Stop();
			this.timer_1.Tick -= new EventHandler(this.timer_1_Tick);
			this.timer_1.Stop();
			this.method_4();
			this.timer_0.Tick -= new EventHandler(this.timer_0_Tick);
			this.timer_0.Stop();
			if (this.detector != null)
			{
				this.detector.Reset();
				this.detector = null;
			}
			if (this.videoSourcePlayer != null)
			{
				this.videoSourcePlayer.NewFrame -= new NewFrameHandler(this.method_2);
				this.videoSourcePlayer.VideoSourceError -= new VideoSourceErrorEventHandler(this.videoSourcePlayer_VideoSourceError);
				this.videoSourcePlayer.SignalToStop();
				int num = 0;
				while (num < 20 && this.videoSourcePlayer.IsRunning)
				{
					Thread.Sleep(100);
					num++;
				}
				if (this.videoSourcePlayer.IsRunning)
				{
					this.videoSourcePlayer.Stop();
				}
				this.videoSourcePlayer = null;
			}
		}
		public void StartRecording()
		{
			if (this.videoSourcePlayer != null)
			{
				this.videoSourcePlayer.EnableRecording = this.bool_5;
				this.videoSourcePlayer.RecordingCodec = this.string_10;
				this.videoSourcePlayer.RecordingRate = this.int_8;
				this.videoSourcePlayer.VideoLengthLimit = this.int_9;
				this.videoSourcePlayer.VideoFolder = this.string_12;
				this.videoSourcePlayer.VideoWriterFinish += new VideoWriterFinishEventHandler(this.videoSourcePlayer_VideoWriterFinish);
				this.videoSourcePlayer.StartRecording();
			}
		}
		public void StopRecording()
		{
			if (this.videoSourcePlayer != null)
			{
				this.videoSourcePlayer.StopRecording();
				this.videoSourcePlayer.VideoWriterFinish -= new VideoWriterFinishEventHandler(this.videoSourcePlayer_VideoWriterFinish);
			}
		}
		private void videoSourcePlayer_VideoWriterFinish(object object_1, string string_13)
		{
			if (this.videoWriterFinishEventHandler_0 != null)
			{
				this.videoWriterFinishEventHandler_0(this, string_13);
			}
		}
		private void method_2(object object_1, ref System.Drawing.Bitmap bitmap_0)
		{
			if (this.int_6 == 0)
			{
				try
				{
					lock (this.object_0)
					{
						if (this.detector != null)
						{
							UnmanagedImage unmanagedImage = UnmanagedImage.FromManagedImage(bitmap_0);
							if (unmanagedImage != null)
							{
								float num = this.detector.ProcessFrame(unmanagedImage);
								float num2 = (float)this.int_5 / 1000f;
								if (this.motionAnalyzingEventHandler_0 != null)
								{
									this.motionAnalyzingEventHandler_0(this, num);
								}
								if (num > num2)
								{
									this.int_1 = 2 * (1000 / this.timer_0.Interval);
									if (this.eventHandler_0 != null)
									{
										this.eventHandler_0(this, new EventArgs());
									}
								}
							}
							unmanagedImage.Dispose();
						}
					}
				}
				catch (Exception ex)
				{
					if (this.cameraErrorEventHandler_0 != null)
					{
						this.cameraErrorEventHandler_0(this, "Error while motion analysis: " + ex.Message);
					}
				}
			}
			if (this.newFrameEventHandler_0 != null)
			{
				this.newFrameEventHandler_0.Invoke(this, new NewFrameEventArgs((System.Drawing.Bitmap)bitmap_0.Clone()));
			}
		}
		private void videoSourcePlayer_VideoSourceError(object sender, VideoSourceErrorEventArgs e)
		{
			if (this.cameraErrorEventHandler_0 != null)
			{
				this.cameraErrorEventHandler_0(this, e.Description);
			}
		}
		private void timer_1_Tick(object sender, EventArgs e)
		{
			if (this.detector != null && this.backgroundWorker_0 != null && !this.backgroundWorker_0.IsBusy)
			{
				this.backgroundWorker_0.RunWorkerAsync();
			}
		}
		private void method_3()
		{
			this.backgroundWorker_0 = new BackgroundWorker();
			this.backgroundWorker_0.WorkerSupportsCancellation = true;
			this.backgroundWorker_0.WorkerReportsProgress = true;
			this.backgroundWorker_0.DoWork += new DoWorkEventHandler(this.backgroundWorker_0_DoWork);
			this.backgroundWorker_0.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker_0_RunWorkerCompleted);
			this.backgroundWorker_0.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker_0_ProgressChanged);
		}
		private void backgroundWorker_0_DoWork(object sender, DoWorkEventArgs e)
		{
			try
			{
				lock (this.object_0)
				{
					if (this.detector != null)
					{
						BackgroundWorker backgroundWorker = sender as BackgroundWorker;
						if (!backgroundWorker.CancellationPending)
						{
							UnmanagedImage currentUnmanagedVideoFrame = this.GetCurrentUnmanagedVideoFrame();
							if (currentUnmanagedVideoFrame != null)
							{
								float num = this.detector.ProcessFrame(currentUnmanagedVideoFrame);
								float num2 = (float)this.int_5 / 1000f;
								if (this.motionAnalyzingEventHandler_0 != null)
								{
									this.motionAnalyzingEventHandler_0(this, num);
								}
								if (num > num2)
								{
									this.int_1 = 2 * (1000 / this.timer_0.Interval);
									if (this.eventHandler_0 != null)
									{
										this.eventHandler_0(this, new EventArgs());
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				if (this.cameraErrorEventHandler_0 != null)
				{
					this.cameraErrorEventHandler_0(this, "Error while motion analysis: " + ex.ToString());
				}
			}
		}
		private void backgroundWorker_0_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
		}
		private void backgroundWorker_0_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
		}
		private void method_4()
		{
			if (this.backgroundWorker_0 != null)
			{
				this.backgroundWorker_0.DoWork -= new DoWorkEventHandler(this.backgroundWorker_0_DoWork);
				this.backgroundWorker_0.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(this.backgroundWorker_0_RunWorkerCompleted);
				this.backgroundWorker_0.ProgressChanged -= new ProgressChangedEventHandler(this.backgroundWorker_0_ProgressChanged);
				this.backgroundWorker_0.CancelAsync();
				this.backgroundWorker_0 = null;
			}
		}
		private void timer_0_Tick(object sender, EventArgs e)
		{
			if (this.int_1 != 0)
			{
				if (this.videoSourcePlayer != null)
				{
					this.videoSourcePlayer.BorderColor = ((this.int_1 % 2 == 1) ? System.Drawing.Color.Black : System.Drawing.Color.Red);
					this.videoSourcePlayer.MotionRectangleColor = ((this.int_1 % 2 == 1) ? System.Drawing.Color.Green : System.Drawing.Color.Red);
				}
				this.int_1--;
			}
		}
		private void timer_2_Tick(object sender, EventArgs e)
		{
		}
		private void method_5(string string_13)
		{
			try
			{
				if (this.ptzsource_0 != null)
				{
					this.ptzsource_0.SignalToStop();
					this.ptzsource_0.Stop();
					this.ptzsource_0 = null;
				}
				this.ptzsource_0 = new PTZSource(string_13);
				this.ptzsource_0.Login = this.string_3;
				this.ptzsource_0.Password = this.string_4;
				this.ptzsource_0.Start();
			}
			catch (Exception ex)
			{
				if (this.cameraErrorEventHandler_0 != null)
				{
					this.cameraErrorEventHandler_0(this, "PTZ: " + ex.Message);
				}
				Thread.Sleep(250);
			}
		}
		private void method_6()
		{
			if (this.cameraType_0 == CameraType.PanasonicPro && this.IsRunning)
			{
				this.method_5("http://" + this.string_2 + "/cgi-bin/keep_alive?mode=jpeg&protocol=http&UID=263&page=20040830203157");
			}
		}
		private void method_7()
		{
			if (this.cameraType_0 == CameraType.PanasonicPro)
			{
				this.method_5(string.Concat(new object[]
				{
					"http://",
					this.string_2,
					"/cgi-bin/jpeg?connect=stop&UID=263&page=20040830203157&ch=",
					this.int_3
				}));
			}
		}
		public void AUXControl(string state)
		{
			if (this.bool_0 && this.IsRunning && this.cameraType_0 == CameraType.PanasonicPro)
			{
				this.method_5("http://" + this.string_2 + "/cgi-bin/pioctrl?almtrm=" + state);
			}
		}
		public void Restart()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/Set?Func=Restart&Kind=1&Data=OK";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/cgi-bin/initial?cmd=reset";
				}
			}
			this.method_5(text);
		}
		public void MoveLeft()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=PanLeft";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=PanLeft";
				}
			}
			this.method_5(text);
		}
		public void MoveUpLeft()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=120&NewPosition.y=90";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=120&NewPosition.y=90";
				}
			}
			this.method_5(text);
		}
		public void MoveUp()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=TiltUp";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=TiltUp";
				}
			}
			this.method_5(text);
		}
		public void MoveUpRight()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=200&NewPosition.y=90";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=200&NewPosition.y=90";
				}
			}
			this.method_5(text);
		}
		public void MoveRight()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=PanRight";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=PanRight";
				}
			}
			this.method_5(text);
		}
		public void MoveDownRight()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=200&NewPosition.y=150";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=200&NewPosition.y=150";
				}
			}
			this.method_5(text);
		}
		public void MoveDown()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=TiltDown";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=TiltDown";
				}
			}
			this.method_5(text);
		}
		public void MoveDownLeft()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=120&NewPosition.y=150";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=Direct&Width=320&Height=240&NewPosition.x=120&NewPosition.y=150";
				}
			}
			this.method_5(text);
		}
		public void MoveHome()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=HomePosition";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=Preset&PresetOperation=Move&Data=1";
				}
			}
			this.method_5(text);
		}
		public void ZoomOut()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=ZoomWide";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=ZoomWide";
				}
			}
			this.method_5(text);
		}
		public void ZoomIn()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=ZoomTele";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/nphControlCamera?Direction=ZoomTele";
				}
			}
			this.method_5(text);
		}
		public void GotoPreset(string presetIndex)
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text = text + "/nphControlCamera?Direction=Preset&PresetOperation=Move&Data=" + presetIndex;
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text = text + "/cgi-bin/camctrl?preset=" + presetIndex;
				}
			}
			this.method_5(text);
		}
		public void SetupPreset(string presetIndex)
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				string text2 = text;
				text = string.Concat(new string[]
				{
					text2,
					"/Set?Func=PresetNamePos&Kind=",
					presetIndex,
					"&Data=Preset",
					presetIndex
				});
			}
			if (this.cameraType_0 == CameraType.PanasonicPro)
			{
				text = text + "/cgi-bin/camposiset?presetset=" + presetIndex;
			}
			this.method_5(text);
		}
		private void method_8()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.Panasonic)
			{
				text += "/nphControlCamera?Direction=PanScan";
			}
			else
			{
				if (this.cameraType_0 == CameraType.PanasonicPro)
				{
					text += "/cgi-bin/camctrl?atmode=atpan";
				}
			}
			this.method_5(text);
		}
		private void method_9()
		{
			string text = "http://" + this.string_2;
			if (this.cameraType_0 == CameraType.PanasonicPro)
			{
				text += "/cgi-bin/camctrl?atmode=off";
			}
			this.method_5(text);
		}
	}
}
